/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.view.labels;

import org.testis.gui.mvc.utilities.ImageFactory;
import org.testis.gui.mvc.utilities.TreeLabel;
import org.testis.utils.tree.DefaultMutableLabelNode;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.TreeCellRenderer;

import org.testis.utils.tree.LabelTreeNode;

/**
 *
 * @author selcuk
 */
public class TwoStateLabelNodeRenderer implements TreeCellRenderer {

    private LabelTreeNode m_labelTreeNode;
    private JCheckBox m_leafRenderer = new JCheckBox();
    protected TreeLabel m_label;
    Color m_selectionBorderColor, m_selectionForeground, m_selectionBackground, m_textForeground, m_textBackground;

    public TwoStateLabelNodeRenderer() {
        Font fontValue;
        fontValue = UIManager.getFont("Tree.font");
        if (fontValue != null) {
            m_leafRenderer.setFont(fontValue);
        }
        Boolean booleanValue = (Boolean) UIManager.get(
                "Tree.drawsFocusBorderAroundIcon");
        m_leafRenderer.setFocusPainted(
                (booleanValue != null) && (booleanValue.booleanValue()));

        m_selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
        m_selectionForeground = UIManager.getColor("Tree.selectionForeground");
        m_selectionBackground = UIManager.getColor("Tree.selectionBackground");
        m_textForeground = UIManager.getColor("Tree.textForeground");
        m_textBackground = UIManager.getColor("Tree.textBackground");
    }

    protected JCheckBox getLeafRenderer() {
        return m_leafRenderer;
    }

    protected LabelTreeNode getLabelTreeNode() {
        return m_labelTreeNode;
    }

    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        String stringValue = tree.convertValueToText(value, selected,
                expanded, leaf, row, false);
        m_leafRenderer.setText(stringValue);
        m_leafRenderer.setSelected(false);
        m_leafRenderer.setEnabled(tree.isEnabled());

        if (selected) {
            m_leafRenderer.setForeground(m_selectionForeground);
            m_leafRenderer.setBackground(m_selectionBackground);
        } else {
            m_leafRenderer.setForeground(m_textForeground);
            m_leafRenderer.setBackground(m_textBackground);
        }

        if (value != null && value instanceof DefaultMutableLabelNode) {
            DefaultMutableLabelNode treeNode = (DefaultMutableLabelNode) value;
            Object userObj = treeNode.getUserObject();
            if (userObj instanceof LabelTreeNode) {
                m_labelTreeNode = (LabelTreeNode) userObj;
                m_leafRenderer.setText(m_labelTreeNode.getName());
                m_leafRenderer.setSelected(m_labelTreeNode.isSelected());

                /*LabelRefined lblRefined = treeNode.getLabel();
                if (lblRefined != null) {
                String imageName = "label.png";
                ImageIcon imageIcon = createImageIcon(
                "images/testdesign/" + imageName);
                m_leafRenderer.setIcon(imageIcon);
                }*/ /*else {
                Icon imageIcon = super.getOpenIcon();
                m_leafRenderer.setIcon(imageIcon);
                }*/
            }

        }
        return m_leafRenderer;
    }

    public static ImageIcon createImageIcon(String path) {
        return ImageFactory.instance().createImageIcon(path);
    }
}
